package com.zxc.demo.controller;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.zxc.demo.dao.UserQxDao;
import com.zxc.demo.model.UserQx;
import com.zxc.demo.model.ZybDcUser;
import com.zxc.demo.service.ZybDcUserService;
import com.zxc.demo.util.AESUtil;
import com.zxc.demo.util.CookieUtil;
import com.zxc.demo.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 消费记录表zyb_dc_consume_record(ZybDcConsumeRecord)表控制层
 *
 * @author makejava
 * @since 2021-02-22 11:25:35
 */
@RestController
@Log4j2
@RequestMapping("user")
@Api(value = "用户接口",tags = {"用户接口"})
@Validated
public class UserController {
    /**
     * 服务对象
     */
    @Resource
    private ZybDcUserService zybDcUserService;
    @Resource
    private UserQxDao userQxDao;


    //@GetMapping("longin")
    @GetMapping("longin")
    @ApiOperation(value = "登录",notes = "登录")
    public R longin(HttpServletResponse response, HttpSession session ,String userName ,String password ) {
        return zybDcUserService.userLogin(response,session,userName,password);
    }
    @GetMapping("setuser")
    @ApiOperation(value = "设置用户",notes = "设置用户")
    public R setuser(HttpServletRequest request,String userName ,String password ,String mobilePhone) {
        Cookie cookieu = CookieUtil.getCookieByName(request,"longin");
        if (cookieu!=null){
            String value = cookieu.getValue();
            if (!StringUtils.isEmpty(value)){
                try {
                    String themobilePhone = AESUtil.decrypt(value);
                    ZybDcUser user = zybDcUserService.getOne(new LambdaQueryWrapper<ZybDcUser>()
                            .eq(ZybDcUser::getIsDelete,0)
                            .eq(ZybDcUser::getMobilePhone, themobilePhone));
                    user.setUserName(userName);
                    user.setPassword(password);
                    user.setMobilePhone(mobilePhone);
                    zybDcUserService.updateById(user);
                    return R.success(user);
                } catch (Exception e) {
                    return R.error("403","登录异常清重新登陆");
                }
            }else {
                return R.error("403","登录已过期");
            }
        }
        return R.error("403","登录已过期");
    }

    @PostMapping("adminSetuser")
    @ApiOperation(value = "管理员设置用户",notes = "管理员设置用户")
    public R adminSetuser(HttpServletRequest request, @RequestBody ZybDcUser setUser) {
        Cookie cookieu = CookieUtil.getCookieByName(request,"longin");
        if (cookieu!=null){
            String value = cookieu.getValue();
            if (!StringUtils.isEmpty(value)){
                try {
                    String themobilePhone = AESUtil.decrypt(value);
                    ZybDcUser user = zybDcUserService.getOne(
                            new LambdaQueryWrapper<ZybDcUser>()
                                    .eq(ZybDcUser::getIsDelete,0)
                                    .eq(ZybDcUser::getMobilePhone, themobilePhone)
                    );
                    QueryWrapper<UserQx> wrapper = new QueryWrapper<UserQx>();
                    wrapper.eq("user_id",user.getId());
                    //查询此用户权限列表
                    List<UserQx> userQxes = userQxDao.selectList(wrapper);
                    boolean canDo = false;
                    for (UserQx userQx : userQxes) {
                        if (userQx.getQxId().equals(1)){//权限id1有用户管理权限
                            canDo = true;
                        }
                    }
                    if (canDo){//有操作用户权限
                        if (setUser.getId()==null){
                            setUser.setRegisterTime(LocalDateTime.now());
                        }
                        boolean b = zybDcUserService.saveOrUpdate(setUser);
                        return R.success(setUser);
                    }else {
                        return R.error("500","无用户操作权限");
                    }
                } catch (Exception e) {
                    return R.error("403","登录异常清重新登陆");
                }
            }else {
                return R.error("403","登录已过期");
            }
        }
        return R.error("403","登录已过期");
    }
    @GetMapping("getUserList")
    @ApiOperation(value = "获取用户列表",notes = "获取用户列表")
    public R getUserList(HttpServletRequest request) {
        List<ZybDcUser> list = zybDcUserService.list(new LambdaQueryWrapper<ZybDcUser>()
                .select(ZybDcUser::getId,ZybDcUser::getUserName,ZybDcUser::getMobilePhone,ZybDcUser::getRegisterTime)
                .eq(ZybDcUser::getIsDelete,0));
        return R.success(list);
    }
    @GetMapping("getuser")
    public R getuser(HttpServletRequest request) {
        Cookie cookieu = CookieUtil.getCookieByName(request,"longin");
        if (cookieu!=null){
            String value = cookieu.getValue();
            if (!StringUtils.isEmpty(value)){
                try {
                    String username = AESUtil.decrypt(value);
                    ZybDcUser user = zybDcUserService.getOne(new LambdaQueryWrapper<ZybDcUser>()
                            .eq(ZybDcUser::getMobilePhone, username));
                    return R.success(user);
                } catch (Exception e) {
                    return R.error("403","登录异常清重新登陆");
                }
            }else {
                return R.error("403","登录已过期");
            }
        }
        return R.error("403","登录已过期");
    }
}